<?php

class airlines extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('user_login/user', 'refresh');
        }

//        $this->load->model('admin_bank_model');
        $this->load->model('airline');
        $this->load->model('user_role');
        $this->load->library('upload');
        $this->load->library('user_id');
        $this->load->helper('user_helper');
    }

    function index() {
        $data['id'] = $this->user_id->id();

        $data['i'] = 1;
        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['rows'] = $this->airline->index();
        $data['content'] = ('airlines/index');
        $this->load->view('template/user', $data);
    }

    function add() {
        $data['id'] = $this->user_id->id();

        $data['access'] = $this->user_role->access($this->user_id->cek());
        $data['content'] = 'airlines/new';
        $this->load->view('template/user', $data);
    }

    function create() {
        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000000';
        $config['max_width'] = '5000';
        $config['max_height'] = '5000';
        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                //array_push($opr,array('error' => $this->upload->display_errors()));


                $name = $this->input->post('name', TRUE);
                $file = $_FILES["userfile"]["name"];

                $date = date('Y-m-d');
                $this->airline->create($name, $file, $date);
            } else {
                //array_push($data,array('error' => 'success'));

                $date = date('y-m-d');
                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $title = $this->input->post('name', TRUE);
                $file = $_FILES["userfile"]["name"];
                $this->airline->create($title, $file, $date);
            }
        } else {
            $pic = '';
        }
    }

    function edit() {
        $data['id'] = $this->user_id->id();

        $id = $this->uri->segment(3);

        //cek
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_edit');

        $data['access'] = $access;
        $data['rows'] = $this->airline->edit($id);
        $data['content'] = 'airlines/edit';
        $this->load->view('template/user', $data);
    }

    function update() {

        $config['upload_path'] = './upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000000';
        $config['max_width'] = '5000';
        $config['max_height'] = '5000';
        $this->upload->initialize($config);

        $pic = '';

        if (isset($_FILES["userfile"]["name"])) {
            if (!$this->upload->do_upload()) {
                //array_push($opr,array('error' => $this->upload->display_errors()));
                $id = $this->input->post('id', TRUE);
                $name = $this->input->post('name', TRUE);
//                $file = $_FILES["userfile"]["name"];
                $file = "";
//
                $this->airline->update($id, $name, $file);
            } else {
                //array_push($data,array('error' => 'success'));

                $tGambar = $this->upload->data();
                $pic = $tGambar["file_name"];
                $id = $this->input->post('id', TRUE);
                $name = $this->input->post('name', TRUE);
                $file = $_FILES["userfile"]["name"];
                $this->airline->update($id, $name, $file);
            }
        } else {
            $pic = '';
        }
    }

    function delete() {
        $access = $this->user_role->access($this->user_id->cek());
        $this->user_role->chek($access, 'bank_delete');
        $id = $this->uri->segment(3);
        $this->airline->delete($id);
    }

    function find() {
        $bandara_a = $this->input->post('bandara_a', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = $this->input->post('bandara_t', TRUE);
        $bandara_t = explode(' ', $bandara_t);
        $count = count($bandara_t);

        if ($count == 2)
            $bandara_t = $bandara_t[0] . "%20" . $bandara_t[1];
        else {
            $bandara_t = $bandara_t[0];
        }
        $date = $this->input->post('date', TRUE);
        $date = explode('/', $date);
        $data['bandara_a'] = $bandara_a;
        $data['bandara_t'] = $bandara_t;
        $data['date'] = $date[1] . $date[0];
        $data['content'] = ('airlines/find');
        $this->load->view('template/airline', $data);
    }

}

